package com.eastedu.superise.handler.ppt;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eastedu.client.ppt.PptVideoClient;
import com.eastedu.data.RequestParameter;
import com.eastedu.data.handler.AbstractPageHistoricalDataHandler;
import com.eastedu.repository.ppt.dto.PptVideoMappingDTO;
import com.eastedu.repository.ppt.mapper.PptVideoRelationMapper;
import com.eastedu.repository.ppt.po.PptVideoRelationPO;
import com.eastedu.utils.CollectionUtils;
import com.eastedu.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 * 批量处理历史ppt和课件关联数据
 *
 * @author zhangzhishuai
 */
@Slf4j
@Service
@DS("ppt")
public class PptVideoHistoricalDataHandler extends AbstractPageHistoricalDataHandler<PptVideoRelationPO, RequestParameter> {
    private final PptVideoClient pptVideoClient;

    /**
     * Instantiates a new Choice question answer parentheses historical data handler.
     *
     * @param pptVideoRelationMapper the question basic info mapper
     * @param pptVideoClient         the ppt video client
     */
    public PptVideoHistoricalDataHandler(PptVideoRelationMapper pptVideoRelationMapper, PptVideoClient pptVideoClient) {
        super(pptVideoRelationMapper);
        this.pptVideoClient = pptVideoClient;
    }

    @Override
    public void callback(PptVideoRelationPO po, Map<String, Object> options) {
        List<PptVideoMappingDTO> pptVideoMappings = JsonUtil.parseList(po.getMatchResult(), PptVideoMappingDTO.class);
        if (CollectionUtils.isEmpty(pptVideoMappings)) {
            log.info("数据为空：{}", po);
            return;
        }
        this.pptVideoClient.saveMappingInfo(pptVideoMappings);
        log.info("保存成功：{}", po);
    }

    @Override
    protected Wrapper<PptVideoRelationPO> getWrapper(Serializable serializable, RequestParameter requestParameter) {
        return Wrappers.<PptVideoRelationPO>lambdaQuery().gt(PptVideoRelationPO::getId, serializable).orderByAsc(PptVideoRelationPO::getId);
    }
}
